<template>
  <div>
    <!-- 编辑、完成、详情显示 -->
    <div class="pt-4">
      <div class="block-title">基本信息</div>
      <el-form ref="basicFormRef" :model="basicForm" :rules="rules" size="small" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="机型" prop="modelType">
              <el-select v-model="basicForm.modelType" clearable disabled>
                <el-option v-for="(item, index) in pageAircraftTypes" :key="'modelType' + index" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="载荷类型" prop="loadName">
              <el-select v-model="basicForm.loadName" clearable disabled @on-change="handleLoadNameChange">
                <el-option v-for="(item, index) in pageLoadNames" :key="'type' + index" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="载荷分类" prop="loadType">
              <el-select v-model="basicForm.loadType" clearable disabled>
                <el-option v-for="(item, index) in pageLoadTypes" :key="'name' + index" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="阶段" prop="stage">
              <el-select v-model="basicForm.stage" clearable disabled>
                <el-option v-for="(item, index) in pageStages" :key="'stage' + index" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本号" prop="version">
              <el-input v-model="basicForm.version" placeholder="请输入版本号" clearable disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="轮次" prop="round">
              <el-input v-model="basicForm.round" placeholder="请输入轮次" clearable disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人" prop="stage">
              <span>{{ basicForm.createBy }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="version">
              <span>{{ basicForm.createTime }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="block-title">参数信息</div>
    </div>
    <div>
      <div class="required-title pos-relative">
        <span class="color-#f56c6c ma-1">*</span>公共参数
        <el-upload
          v-if="type !== 'view' && type !== 'complate'"
          class="pos-absolute pos-left-20 pos-top--1"
          :action="upload.url"
          :headers="upload.headers"
          :limit="1"
          :on-success="handleSuccess"
          :show-file-list="false"
        >
          <el-button type="text" size="small">导入数据</el-button>
        </el-upload>
      </div>
      <div class="block-element">
        <div class="card-title">累计站位：</div>
        <div class="card-btn">
          <VersionUpload
            key="station"
            ref="cumulativeStationRef"
            v-model="paramsForm.cumulative_station_5_0"
            :limit="17"
            :disabled="type === 'view' || type === 'complate'"
            @on-change="handlePublicParamChange('cumulative_station_5_0')"
          />
        </div>
      </div>
      <div class="block-element2">
        <div class="card-title">刚轴信息：</div>
        <div class="card-content">
          <div class="flex flex-items-center pt3">
            <div class="card-label">垂尾刚轴原点：</div>
            <VersionParams
              key="origin_location_cw"
              v-model="paramsForm.origin_location_cw"
              :type="type"
              title="输入垂尾刚轴原点参数"
              @on-change="handlePublicParamChange('origin_location_cw')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">垂尾转换矩阵：</div>
            <VersionParams
              key="reverse_array_cw"
              v-model="paramsForm.reverse_array_cw"
              :type="type"
              title="输入垂尾转换矩阵参数"
              @on-change="handlePublicParamChange('reverse_array_cw')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左平尾刚轴原点：</div>
            <VersionParams
              key="origin_location_pwl"
              v-model="paramsForm.origin_location_pwl"
              :type="type"
              title="输入左平尾刚轴原点参数"
              @on-change="handlePublicParamChange('origin_location_pwl')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左平尾转换矩阵：</div>
            <VersionParams
              key="reverse_array_pwl"
              v-model="paramsForm.reverse_array_pwl"
              :type="type"
              title="输入左平尾转换矩阵参数"
              @on-change="handlePublicParamChange('reverse_array_pwl')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右平尾刚轴原点：</div>
            <VersionParams
              key="origin_location_pwr"
              v-model="paramsForm.origin_location_pwr"
              :type="type"
              title="输入右平尾刚轴原点参数"
              @on-change="handlePublicParamChange('origin_location_pwr')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右平尾转换矩阵：</div>
            <VersionParams
              key="reverse_array_pwr"
              v-model="paramsForm.reverse_array_pwr"
              :type="type"
              title="输入右平尾转换矩阵参数"
              @on-change="handlePublicParamChange('reverse_array_pwr')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左机翼内刚轴原点：</div>
            <VersionParams
              key="origin_location_inner_jyl"
              v-model="paramsForm.origin_location_inner_jyl"
              :type="type"
              title="输入左机翼内刚轴原点参数"
              @on-change="handlePublicParamChange('origin_location_inner_jyl')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左机翼内转换矩阵：</div>
            <VersionParams
              key="reverse_array_inner_jyl"
              v-model="paramsForm.reverse_array_inner_jyl"
              :type="type"
              title="输入左机翼内转换矩阵参数"
              @on-change="handlePublicParamChange('reverse_array_inner_jyl')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左机翼外刚轴原点：</div>
            <VersionParams
              key="origin_location_outer_jyl"
              v-model="paramsForm.origin_location_outer_jyl"
              :type="type"
              title="输入左机翼外刚轴原点参数"
              @on-change="handlePublicParamChange('origin_location_outer_jyl')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左机翼外转换矩阵：</div>
            <VersionParams
              key="reverse_array_outer_jyl"
              v-model="paramsForm.reverse_array_outer_jyl"
              :type="type"
              title="输入左机翼外转换矩阵参数"
              @on-change="handlePublicParamChange('reverse_array_outer_jyl')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右机翼内刚轴原点：</div>
            <VersionParams
              key="origin_location_inner_jyr"
              v-model="paramsForm.origin_location_inner_jyr"
              :type="type"
              title="输入右机翼内刚轴原点参数"
              @on-change="handlePublicParamChange('origin_location_inner_jyr')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右机翼内转换矩阵：</div>
            <VersionParams
              key="reverse_array_inner_jyr"
              v-model="paramsForm.reverse_array_inner_jyr"
              :type="type"
              title="输入右机翼内转换矩阵参数"
              @on-change="handlePublicParamChange('reverse_array_inner_jyr')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右机翼外刚轴原点：</div>
            <VersionParams
              key="origin_location_outer_jyr"
              v-model="paramsForm.origin_location_outer_jyr"
              :type="type"
              title="输入右机翼外刚轴原点参数"
              @on-change="handlePublicParamChange('origin_location_outer_jyr')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右机翼外转换矩阵：</div>
            <VersionParams
              key="reverse_array_outer_jyr"
              v-model="paramsForm.reverse_array_outer_jyr"
              :type="type"
              title="输入右机翼外转换矩阵参数"
              @on-change="handlePublicParamChange('reverse_array_outer_jyr')"
            />
          </div>
        </div>
      </div>
      <div class="block-element2">
        <div class="card-title">集中点分载：</div>

        <div class="card-content">
          <div class="flex flex-items-center pt3">
            <div class="card-label">K值：</div>
            <div class="w-100">
              <el-input v-model="paramsForm.k" :disabled="type === 'view' || type === 'complate'" size="small" @blur="handlePublicParamChange('k')" />
            </div>
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">前起坐标1：</div>
            <VersionParams
              key="location_qj_1"
              v-model="paramsForm.location_qj_1"
              :type="type"
              title="输入前起坐标1参数"
              @on-change="handlePublicParamChange('location_qj_1')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">前起节点1：</div>
            <VersionParams
              key="node_mark_qj_1"
              v-model="paramsForm.node_mark_qj_1"
              :type="type"
              title="输入前起节点1参数"
              @on-change="handlePublicParamChange('node_mark_qj_1')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">前起坐标2：</div>
            <VersionParams
              key="location_qj_2"
              v-model="paramsForm.location_qj_2"
              :type="type"
              title="输入前起坐标2参数"
              @on-change="handlePublicParamChange('location_qj_2')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">前起节点2：</div>
            <VersionParams
              key="node_mark_qj_2"
              v-model="paramsForm.node_mark_qj_2"
              :type="type"
              title="输入前起节点2参数"
              @on-change="handlePublicParamChange('node_mark_qj_2')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">前起坐标3：</div>
            <VersionParams
              key="location_qj_3"
              v-model="paramsForm.location_qj_3"
              :type="type"
              title="输入前起坐标3参数"
              @on-change="handlePublicParamChange('location_qj_3')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">前起节点3：</div>
            <VersionParams
              key="node_mark_qj_3"
              v-model="paramsForm.node_mark_qj_3"
              :type="type"
              title="输入前起节点3参数"
              @on-change="handlePublicParamChange('node_mark_qj_3')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">前起坐标4：</div>
            <VersionParams
              key="location_qj_4"
              v-model="paramsForm.location_qj_4"
              :type="type"
              title="输入前起坐标4参数"
              @on-change="handlePublicParamChange('location_qj_4')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">前起节点4：</div>
            <VersionParams
              key="node_mark_qj_4"
              v-model="paramsForm.node_mark_qj_4"
              :type="type"
              title="输入前起节点4参数"
              @on-change="handlePublicParamChange('node_mark_qj_4')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左短舱坐标：</div>
            <VersionParams
              key="culm_list_1"
              v-model="paramsForm.culm_list_1"
              :type="type"
              title="输入左短舱坐标参数"
              @on-change="handlePublicParamChange('culm_list_1')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左短舱节点：</div>
            <VersionParams
              key="node_mark1"
              v-model="paramsForm.node_mark1"
              :type="type"
              title="输入左短舱节点参数"
              @on-change="handlePublicParamChange('node_mark1')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右短舱坐标：</div>
            <VersionParams
              key="culm_list_2"
              v-model="paramsForm.culm_list_2"
              :type="type"
              title="输入右短舱坐标参数"
              @on-change="handlePublicParamChange('culm_list_2')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右短舱节点：</div>
            <VersionParams
              key="node_mark2"
              v-model="paramsForm.node_mark2"
              :type="type"
              title="输入右短舱节点参数"
              @on-change="handlePublicParamChange('node_mark2')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左主起坐标：</div>
            <VersionParams
              key="culm_list_3"
              v-model="paramsForm.culm_list_3"
              :type="type"
              title="输入左主起坐标参数"
              @on-change="handlePublicParamChange('culm_list_3')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">左主起节点：</div>
            <VersionParams
              key="node_mark_3"
              v-model="paramsForm.node_mark_3"
              :type="type"
              title="输入左主起节点参数"
              @on-change="handlePublicParamChange('node_mark_3')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右主起坐标：</div>
            <VersionParams
              key="culm_list_4"
              v-model="paramsForm.culm_list_4"
              :type="type"
              title="输入右主起坐标参数"
              @on-change="handlePublicParamChange('culm_list_4')"
            />
          </div>
          <div class="flex flex-items-center pt3">
            <div class="card-label">右主起节点：</div>
            <VersionParams
              key="node_mark_4"
              v-model="paramsForm.node_mark_4"
              :type="type"
              title="输入右主起节点参数"
              @on-change="handlePublicParamChange('node_mark_4')"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 地面载荷 -->
    <div v-if="basicForm.loadType === '地面载荷'">
      <div class="required-title mt-2"><span class="color-#f56c6c ma-1">*</span>特有参数</div>
      <div class="block-element3">
        <div class="flex flex-items-center">
          <div class="card-title">平尾接入点：</div>
          <VersionParams key="xunidian1" v-model="specificParamForm.xunidian1" :type="type" @on-change="handleSpecificParamChange('xunidian1')" />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">垂尾接入点：</div>
          <VersionParams key="xunidian2" v-model="specificParamForm.xunidian2" :type="type" @on-change="handleSpecificParamChange('xunidian2')" />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">左主起所在行：</div>
          <el-input
            key="zhuqi_L"
            v-model="specificParamForm.zhuqi_L"
            style="width: 200px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            placeholder="请输入左主起所在行，如：1471"
            @change="handleSpecificParamChange('zhuqi_L')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">右主起所在行：</div>
          <el-input
            key="zhuqi_R"
            v-model="specificParamForm.zhuqi_R"
            placeholder="请输入右主起所在行，如：2100"
            style="width: 200px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            @change="handleSpecificParamChange('zhuqi_R')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">前起所在行：</div>
          <el-input
            key="qianqi"
            v-model="specificParamForm.qianqi"
            placeholder="请输入前起所在行，如：281"
            style="width: 200px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            @change="handleSpecificParamChange('qianqi')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">左吊挂所在行：</div>
          <VersionRange key="diaogua_L" v-model="specificParamForm.diaogua_L" :type="type" @on-change="handleSpecificParamChange('diaogua_L')" />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">右吊挂所在行：</div>
          <VersionRange key="diaogua_R" v-model="specificParamForm.diaogua_R" :type="type" @on-change="handleSpecificParamChange('diaogua_R')" />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">机身交点位置：</div>
          <versionLocation
            key="jishen_jiaodian_station"
            v-model="specificParamForm.jishen_jiaodian_station"
            :type="type"
            @on-change="handleSpecificParamChange('jishen_jiaodian_station')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">左机翼交点位置：</div>
          <versionLocation
            key="jiyi_jiaodian_station_L"
            v-model="specificParamForm.jiyi_jiaodian_station_L"
            :type="type"
            @on-change="handleSpecificParamChange('jiyi_jiaodian_station_L')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">右机翼交点位置：</div>
          <versionLocation
            key="jiyi_jiaodian_station_R"
            v-model="specificParamForm.jiyi_jiaodian_station_R"
            :type="type"
            @on-change="handleSpecificParamChange('jiyi_jiaodian_station_R')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">部段标识：</div>
          <el-input
            key="a1"
            v-model="specificParamForm.a1"
            placeholder="请输入右机翼交点位置，如：CW,PW,..."
            style="width: 800px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            @change="handleSpecificParamChange('a1')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">部段名称：</div>
          <el-input
            key="a2"
            v-model="specificParamForm.a2"
            placeholder="请输入右机翼交点位置，如：垂尾_结构,平尾_结构,..."
            style="width: 800px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            @change="handleSpecificParamChange('a2')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">机身交接站位：</div>
          <el-input
            key="markstation"
            v-model="specificParamForm.markstation"
            placeholder="请输入机身交接站位，如：56"
            style="width: 200px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            @change="handleSpecificParamChange('markstation')"
          />
        </div>
      </div>
    </div>
    <!-- 动突风、动着陆、动滑行 -->
    <div v-else-if="basicForm.loadType === '动突风' || basicForm.loadType === '动着陆' || basicForm.loadType === '动滑行'">
      <div class="required-title mt-2"><span class="color-#f56c6c ma-1">*</span>特有参数</div>
      <div class="block-element3">
        <div class="flex flex-items-center">
          <div class="card-title">机身交接站位：</div>
          <el-input
            key="markstation"
            v-model="specificParamForm.markstation"
            placeholder="请输入机身交接站位，如：56"
            style="width: 200px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            @change="handleSpecificParamChange('markstation')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">机翼转轴点：</div>
          <el-input
            key="mark1"
            v-model="specificParamForm.mark1"
            placeholder="请输入机翼转轴点，如：56"
            style="width: 200px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            @change="handleSpecificParamChange('mark1')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">总站位点：</div>
          <el-input
            key="mark2"
            v-model="specificParamForm.mark2"
            placeholder="请输入总站位点，如：56"
            style="width: 200px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            @change="handleSpecificParamChange('mark2')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">吊挂所在站位点：</div>
          <el-input
            key="num"
            v-model="specificParamForm.num"
            placeholder="请输入总站位点，如：56"
            style="width: 200px"
            :disabled="type === 'view' || type === 'complate'"
            size="small"
            @change="handleSpecificParamChange('num')"
          />
        </div>
      </div>
    </div>
    <div v-else>
      <div class="required-title mt-2"><span class="color-#f56c6c ma-1">*</span>特有参数</div>
      <div class="block-element3">
        <div class="flex flex-items-center">
          <div class="card-title">辅助拆分表格：</div>
          <VersionUpload
            key="split_excle_file_2_0"
            ref="splitExcleRef"
            v-model="specificParamForm.split_excle_file_2_0"
            type="specific"
            :limit="100"
            accept=".xls,.xlsx"
            :disabled="type === 'view' || type === 'complate'"
            @on-change="handleSpecificParamChange('split_excle_file_2_0')"
          />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">平尾接入点：</div>
          <VersionParams key="xunidian1" v-model="specificParamForm.xunidian1" :type="type" @on-change="handleSpecificParamChange('xunidian1')" />
        </div>
        <div class="flex flex-items-center">
          <div class="card-title">垂尾接入点：</div>
          <VersionParams key="xunidian2" v-model="specificParamForm.xunidian2" :type="type" @on-change="handleSpecificParamChange('xunidian2')" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="versionDetailForm" lang="ts">
import {
  getVersionDetail,
  addPublicParameter,
  editPublicParameter,
  addSpecificParameter,
  editSpecificParameter,
  uploadPublicParameter,
  uploadSpecificParameter,
  publicParameterList,
  specificParameterList,
  upgradePublicParameter,
  upgradeSpecificParameter
} from '@/api/loadProcessingAlgorithm/loadVersionManagement/index';
import VersionUpload from './versionUpload.vue';
import VersionParams from './versionParams.vue';
import VersionRange from './versionRange.vue';
import versionLocation from './versionLocation.vue';
import { globalHeaders } from '@/utils/request';
import { loadNames, stages, aircraftTypes } from '@/api/loadProcessingAlgorithm/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { type, dataId, oldId } = defineProps({
  dataId: String,
  type: String,
  oldId: String
});
const basicFormRef = ref<ElFormInstance>();
const cumulativeStationRef = ref(null);
const splitExcleRef = ref(null);
const pageLoadNames = ref(loadNames);
const pageLoadTypes = ref([]);
const changePublicParams = ref([]);
const changeSpecificParams = ref([]);
const pageStages = ref(stages);
const pageAircraftTypes = ref(aircraftTypes);
const upload = reactive<UploadOption>({
  headers: globalHeaders(),
  url: import.meta.env.VITE_APP_BASE_API + '/business/version/upload'
});
const basicForm = reactive({
  id: '',
  loadType: '',
  loadName: '',
  stage: '',
  version: '',
  modelType: '',
  round: '',
  createBy: '',
  createTime: ''
});
const oldForm = reactive({
  id: '',
  loadType: '',
  loadName: '',
  stage: '',
  version: '',
  modelType: '',
  round: '',
  createBy: '',
  createTime: ''
});
const paramsForm = reactive({
  cumulative_station_5_0: '', // 累积站位
  origin_location_cw: '', // 垂尾刚轴原点
  reverse_array_cw: '', //垂尾转换矩阵
  origin_location_pwl: '', //左平尾刚轴原点
  reverse_array_pwl: '', //	左平尾转换矩阵
  origin_location_pwr: '', //	右平尾刚轴原点
  reverse_array_pwr: '', //	右平尾转换矩阵
  origin_location_inner_jyl: '', //	左机翼内刚轴原点
  reverse_array_inner_jyl: '', //	左机翼内转换矩阵
  origin_location_outer_jyl: '', //	左机翼外刚轴原点
  reverse_array_outer_jyl: '', //	左机翼外转换矩阵
  origin_location_inner_jyr: '', //	右机翼内刚轴原点
  reverse_array_inner_jyr: '', //	右机翼内转换矩阵
  origin_location_outer_jyr: '', //	右机翼外刚轴原点
  reverse_array_outer_jyr: '', //	右机翼外转换矩阵
  k: '', //	k值
  location_qj_1: '', //	前起坐标1
  node_mark_qj_1: '', //	前起节点1
  location_qj_2: '', //	前起坐标2
  node_mark_qj_2: '', //	前起节点2
  location_qj_3: '', //	前起坐标3
  node_mark_qj_3: '', //	前起节点3
  location_qj_4: '', //	前起坐标4
  node_mark_qj_4: '', //	前起节点4
  culm_list_1: '', //	左短舱坐标
  node_mark1: '', //	左短舱节点
  culm_list_2: '', //	右短舱坐标
  node_mark2: '', //	右短舱节点
  culm_list_3: '', //	左主起坐标
  node_mark_3: '', //	左主起节点
  culm_list_4: '', //	右主起坐标
  node_mark_4: '' //	右主起节点
});
const specificParamForm = ref({});
//飞行垂向、飞行侧向、着落1g、突风1g
const specificParamForm1 = reactive({
  split_excle_file_2_0: '', //	辅助拆分表格
  xunidian1: '', //	平尾接入点
  xunidian2: '' //	垂尾接入点
});
//地面载荷
const specificParamForm2 = reactive({
  xunidian1: '', //	平尾接入点
  xunidian2: '', //	垂尾接入点
  zhuqi_L: '', //	左主起所在行
  zhuqi_R: '', // 右主起所在行
  qianqi: '', // 前起所在行
  diaogua_L: '', // 左吊挂所在行
  diaogua_R: '', // 右吊挂所在行
  jishen_jiaodian_station: '', //机身交点位置
  jiyi_jiaodian_station_L: '', //	左机翼交点位置
  jiyi_jiaodian_station_R: '', //	右机翼交点位置
  a1: '', //	部段标识
  a2: '', //	部段名称
  markstation: '' //	机身交接站位
});
// 动突风、动着陆、动滑行
const specificParamForm3 = reactive({
  markstation: '', //	机身交接站位
  mark1: '', //	机翼转轴点
  mark2: '', //	总站位点
  num: '' //	吊挂所在站位点
});
const paramIdForm = reactive({});
const specificParamIdForm = reactive({});
const rules = reactive({
  loadType: [{ required: true, message: '载荷分类不能为空', trigger: 'change' }],
  loadName: [{ required: true, message: '载荷类型不能为空', trigger: 'change' }],
  stage: [{ required: true, message: '阶段不能为空', trigger: 'change' }],
  version: [{ required: true, message: '版本不能为空', trigger: 'blur' }],
  modelType: [{ required: true, message: '机型不能为空', trigger: 'change' }],
  round: [{ required: true, message: '轮次不能为空', trigger: 'blur' }]
});
const handleLoadNameChange = () => {
  if (basicForm.loadName) {
    const obj = pageLoadNames.value.find((item) => item.label === basicForm.loadName);
    pageLoadTypes.value = obj.children;
  } else {
    pageLoadNames.value.forEach((item) => {
      pageLoadTypes.value = [...pageLoadTypes.value, ...item.children];
    });
  }
};
const submitData = async () => {
  for (const key in paramsForm) {
    if (!paramsForm[key]) {
      proxy?.$modal.msgError(`请输入完整信息！`);
      return false;
    }
  }
  for (const key in specificParamForm.value) {
    if (!specificParamForm.value[key]) {
      proxy?.$modal.msgError(`请输入完整信息！`);
      return false;
    }
  }
  await savePublicParamForm();
  await uploadCumulativeStation();
  await uploadSplitExcleFile();
  await saveSpecificParamForm();
  return true;
};
// 保存公有参数
const savePublicParamForm = async () => {
  const paramObj = {
    stage: basicForm.stage,
    version: basicForm.version,
    baseLhamPublicParameterBos: []
  };
  changePublicParams.value.forEach((key) => {
    if (key === 'k' || key.includes('node_')) {
      const obj = {
        id: type === 'edit' ? paramIdForm[key] : undefined,
        parameterName: key,
        parameterType: '3',
        value: paramsForm[key]
      };
      paramObj.baseLhamPublicParameterBos.push(obj);
    } else if (key === 'cumulative_station_5_0') {
      const obj = {
        id: type === 'edit' ? paramIdForm[key] : undefined,
        parameterName: key,
        parameterType: '1',
        value: paramsForm[key]
      };
      paramObj.baseLhamPublicParameterBos.push(obj);
    } else {
      const obj = {
        id: type === 'edit' ? paramIdForm[key] : undefined,
        parameterName: key,
        parameterType: '2',
        value: paramsForm[key]
      };
      paramObj.baseLhamPublicParameterBos.push(obj);
    }
  });
  if (paramObj.baseLhamPublicParameterBos.length) {
    if (type === 'edit') {
      await editPublicParameter(paramObj.baseLhamPublicParameterBos);
    } else {
      await addPublicParameter(paramObj);
    }
  }
};
// 累计站位上传
const uploadCumulativeStation = async () => {
  const files = cumulativeStationRef.value.getData();
  if (changePublicParams.value.includes('cumulative_station_5_0') && files.length) {
    const formData = new FormData();
    formData.append('loadType', basicForm.loadType);
    formData.append('modelType', basicForm.modelType);
    formData.append('stage', basicForm.stage);
    formData.append('version', basicForm.version);
    formData.append('parameterName', 'cumulative_station_5_0');
    files.forEach((item) => {
      formData.append('files', item.raw);
    });
    if (type === 'edit') {
      formData.append('id', specificParamIdForm['cumulative_station_5_0']);
    }
    await uploadPublicParameter(formData);
    return;
  }
  if (type === 'upgrade') {
    const formData = new FormData();
    formData.append('oldVersionId', oldId);
    formData.append('newVersionId', dataId);
    upgradePublicParameter(formData);
  }
};
// 保存特有参数
const saveSpecificParamForm = async () => {
  const arr = [];
  changeSpecificParams.value.forEach((key) => {
    if (key === 'split_excle_file_2_0') {
      const obj = {
        id: type === 'edit' ? specificParamIdForm[key] : undefined,
        loadcaseVersionId: dataId,
        parameterName: key,
        parameterType: '1',
        value: specificParamForm.value[key]
      };
      arr.push(obj);
    } else if (key === 'a1' || key === 'a2') {
      const obj = {
        id: type === 'edit' ? specificParamIdForm[key] : undefined,
        loadcaseVersionId: dataId,
        parameterName: key,
        parameterType: '4',
        value: specificParamForm.value[key]
      };
      arr.push(obj);
    } else if (key === 'xunidian1' || key === 'xunidian2') {
      const obj = {
        id: type === 'edit' ? specificParamIdForm[key] : undefined,
        loadcaseVersionId: dataId,
        parameterName: key,
        parameterType: '2',
        value: specificParamForm.value[key]
      };
      arr.push(obj);
    } else {
      const obj = {
        id: type === 'edit' ? specificParamIdForm[key] : undefined,
        loadcaseVersionId: dataId,
        parameterName: key,
        parameterType: '3',
        value: specificParamForm.value[key]
      };
      arr.push(obj);
    }
  });
  if (arr.length) {
    if (type === 'edit') {
      await editSpecificParameter(arr);
    } else {
      await addSpecificParameter(arr);
    }
  }
};
// 辅助拆分表格上传
const uploadSplitExcleFile = async () => {
  let files = [];
  if (splitExcleRef.value) {
    files = splitExcleRef.value.getData();
    if (!files) {
      return false;
    }
  }
  if (changeSpecificParams.value.includes('split_excle_file_2_0') && files.length) {
    const formData = new FormData();
    formData.append('loadcaseVersionId', dataId);
    formData.append('parameterName', 'split_excle_file_2_0');
    files.forEach((item) => {
      formData.append('files', item.raw);
    });
    if (type === 'edit') {
      formData.append('id', specificParamIdForm['split_excle_file_2_0']);
    }
    await uploadSpecificParameter(formData);
    return;
  }
  if (type === 'upgrade') {
    const formData = new FormData();
    formData.append('oldVersionId', oldId);
    formData.append('newVersionId', dataId);
    upgradeSpecificParameter(formData);
  }
};
const handlePublicParamChange = (val) => {
  if (!changePublicParams.value.includes(val)) {
    changePublicParams.value.push(val);
  }
};
const handleSpecificParamChange = (val) => {
  if (!changeSpecificParams.value.includes(val)) {
    changeSpecificParams.value.push(val);
  }
};
const getPublicParameters = async (form) => {
  const obj = {
    stage: form.stage,
    version: form.version,
    pageNum: 1,
    pageSize: 99
  };
  const { rows } = await publicParameterList(obj);
  rows.forEach((item) => {
    paramsForm[item.parameterName] = item.value;
    paramIdForm[item.parameterName] = item.id;
  });
  return rows;
};
const getSpecificParameters = async (dataId) => {
  const obj = {
    loadcaseVersionId: dataId,
    pageNum: 1,
    pageSize: 99
  };
  const { rows } = await specificParameterList(obj);
  rows.forEach((item) => {
    specificParamForm.value[item.parameterName] = item.value;
    specificParamIdForm[item.parameterName] = item.id;
  });
  return rows;
};
// 删除参数
const getPublicParamIds = () => {
  const publicIds = [];
  for (const key in paramIdForm) {
    if (paramIdForm[key]) {
      publicIds.push(paramIdForm[key]);
    }
  }
  return publicIds;
};
const getSpecificParamIds = () => {
  const specificIds = [];
  for (const key in specificParamIdForm) {
    if (specificParamIdForm[key]) {
      specificIds.push(specificParamIdForm[key]);
    }
  }
  return specificIds;
};
const initData = async () => {
  // 基础信息
  const { data } = await getVersionDetail(dataId);
  for (const key in basicForm) {
    basicForm[key] = data[key];
  }
  if (basicForm.loadType === '地面载荷') {
    specificParamForm.value = specificParamForm2;
  } else if (basicForm.loadType === '动突风' || basicForm.loadType === '动着陆' || basicForm.loadType === '动滑行') {
    specificParamForm.value = specificParamForm3;
  } else {
    specificParamForm.value = specificParamForm1;
  }
  // 公共参数
  await getSpecificParameters(dataId);
  await getPublicParameters(basicForm);
};
const initData2 = async () => {
  // 基础信息
  const { data } = await getVersionDetail(oldId);
  for (const key in oldForm) {
    oldForm[key] = data[key];
  }
  if (basicForm.loadType === '地面载荷') {
    specificParamForm.value = specificParamForm2;
  } else if (basicForm.loadType === '动突风' || basicForm.loadType === '动着陆' || basicForm.loadType === '动滑行') {
    specificParamForm.value = specificParamForm3;
  } else {
    specificParamForm.value = specificParamForm1;
  }
  // 公共参数
  const data1 = await getSpecificParameters(oldId);
  const data2 = await getPublicParameters(oldForm);
  data1.forEach((item) => {
    if (!changeSpecificParams.value.includes(item.parameterName)) {
      changeSpecificParams.value.push(item.parameterName);
    }
  });
  data2.forEach((item) => {
    if (!changePublicParams.value.includes(item.parameterName)) {
      changePublicParams.value.push(item.parameterName);
    }
  });
};
const handleSuccess = (res, file) => {
  if (res.code === 200) {
    for (const key in res.data) {
      changePublicParams.value.push(key);
      paramsForm[key] = res.data[key];
    }
    proxy?.$modal.msgSuccess(`导入成功！`);
  } else {
    proxy?.$modal.msgError(`导入错误：${res.msg}`);
  }
};
defineExpose({
  submitData,
  getPublicParamIds,
  getSpecificParamIds
});
onMounted(async () => {
  if (type === 'upgrade' && oldId) {
    const { data } = await getVersionDetail(dataId);
    for (const key in basicForm) {
      basicForm[key] = data[key];
    }
    if (basicForm.loadType === '地面载荷') {
      specificParamForm.value = specificParamForm2;
    } else if (basicForm.loadType === '动突风' || basicForm.loadType === '动着陆' || basicForm.loadType === '动滑行') {
      specificParamForm.value = specificParamForm3;
    } else {
      specificParamForm.value = specificParamForm1;
    }
    initData2();
  } else {
    initData();
  }
});
</script>
<style lang="scss" scoped>
.block-title {
  color: #1890ff;
  font-size: 14px;
  padding-bottom: 12px;
  display: flex;
  align-items: center;
  margin-left: 8px;
}
.block-title::before {
  content: '';
  width: 3px;
  height: 15px;
  background-color: #48a1f5;
  margin-right: 8px;
}
.required-title {
  color: #606266;
  padding-bottom: 12px;
  font-weight: 700;
  font-size: 12px;
}
.block-element {
  border: 1px solid #e4e4e4;
  display: flex;
  align-items: center;
}
.block-element2 {
  border: 1px solid #e4e4e4;
  display: flex;
  padding-bottom: 12px;
  margin-top: 8px;
}
.block-element3 {
  border: 1px solid #e4e4e4;
}
.card-title {
  width: 110px;
  text-align: right;
  padding: 12px 0;
  font-size: 12px;
}
.card-content {
  width: calc(100% - 100px);
  display: flex;
  flex-wrap: wrap;
}
.card-label {
  width: 130px;
  font-size: 12px;
  text-align: right;
}
:deep(.el-button.el-button--primary.el-button--default.is-disabled.is-link) {
  color: #606266;
}
</style>
